package com.uhu.code;

/**
 * 238. 除自身以外数组的乘积
 * https://leetcode.cn/problems/product-of-array-except-self/?envType=study-plan-v2&envId=top-100-liked
 */
public class Solution238 {
    public int[] productExceptSelf(int[] nums) {
        int left[] = new int[nums.length];
        int right[] = new int[nums.length];
        left[0] = nums[0];
        right[nums.length - 1] = nums[nums.length - 1];
        for(int i = 1; i < nums.length; i++) {
            left[i] = left[i-1]*nums[i];
        }
        for(int i = nums.length - 2; i > 0; i--) {
            right[i] = right[i+1]*nums[i];
        }
        for(int i = 0; i < nums.length; i++) {
            if(i == 0) {
                nums[i] = right[i + 1];
            }else if(i == nums.length - 1) {
                nums[i] = left[i - 1];
            }else {
                nums[i] = left[i - 1]*right[i + 1];
            }
        }
        return nums;
    }
}
